﻿using HyEduOnline.Entities;
using HyEduOnline.Server.Admin;
using HyEduOnline.Server.Teacher;
using HyEduOnline.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HyEduOnline.Controllers
{
    [UserAuthorizeTea]
    public class TeacherMangerController : Controller
    {
        //
        // GET: /TeacherManger/

        public ActionResult Index()
        {
            return View();
        }



        /// <summary>
        /// 在线试题库试题类表
        /// </summary>
        /// <returns></returns>
        public ActionResult TestLib(int CourseID)
        {
            ViewBag.CourseID = CourseID;
            string whereClause = "I_ParentID=0  and I_DeanCourseID=" + CourseID;
            ViewBag.Charpter = new TeaCatalogDAO().GetByPage(whereClause, 0, int.MaxValue);

            return View();
        }

        /// <summary>
        ///试题导向
        /// </summary>
        /// <param name="CourseID"></param>
        /// <returns></returns>
        public ActionResult Wizard(int CourseID )
        {
            ViewBag.CourseID = CourseID;
            TbDeanCourse tempCouse = new DeanCourseDAO().GetByID(CourseID);
            if (tempCouse != null)
            {
                ViewBag.name = tempCouse.SCourseName;  //传递课程的课程名到页面
            }
            string whereClause = "I_ParentID=0  and I_DeanCourseID=" + CourseID;
            ViewBag.Charpter = new TeaCatalogDAO().GetByPage(whereClause, 0, int.MaxValue);

            return View();
        }

        /// <summary>
        /// 更新试题内容，在试题列表上点击“操作”时使用
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateTestLib(int CourseID )
        {

            ViewBag.CourseID = CourseID;
            string whereClause = "I_ParentID=0  and I_DeanCourseID=" + CourseID;
            ViewBag.Charpter = new TeaCatalogDAO().GetByPage(whereClause, 0, int.MaxValue);

            return View();
        }

       /// <summary>
       /// 试卷列表
       /// </summary>
       /// <returns></returns>
        public ActionResult TestLibExamView(int CourseID)
        {
            ViewBag.CourseID = CourseID;
            return View();
        }
        /// <summary>
        /// 新建试卷
        /// </summary>
        /// <returns></returns>
        public ActionResult PaperCreateView(int CourseID)
        {
            ViewBag.CourseID = CourseID;
            return View();
        }


        /// <summary>
        /// 新建试卷
        /// </summary>
        /// <returns></returns>
        public ActionResult ExportTestLib()
        {
           
            return View();
        }


        /// <summary>
        /// 试卷的预览  点击试卷列表中对应试题的操作时跳转
        /// </summary>
        /// <returns></returns>
        public ActionResult TestPaperPreview(int PaperID)
        {

            ViewBag.PaperID = PaperID;  //传递试卷的id到页面

            //传递CourseId到页面
            TbTestLibPaper tempPaper = new TestLibPaperDAO().GetByID(PaperID);
            if (tempPaper != null)
            {
                ViewBag.TestName = tempPaper.STestName;  //传递试卷名到页面
                ViewBag.CourseID = tempPaper.ICourseId; //传递CourseId到页面
                if (tempPaper.ITestType == 1)
                {
                    ViewBag.TestType = "闭卷";// 传递该试卷的考核方式到页面
                }
                else
                {
                    ViewBag.TestType = "开卷";// 传递该试卷的考核方式到页面
                }
                string courseId = tempPaper.ICourseId.ToString();
                int courseID = int.Parse(courseId);

                TbDeanCourse tempCourse = new DeanCourseDAO().GetByID(courseID);   //在课程表中根据ID查询该试卷所属的科目
                if (tempCourse != null)
                {
                    ViewBag.CourseName = tempCourse.SCourseName;  //传递考试科目（课程名）到页面
                }
            }


            List<TestPaperExt> tempStrategyContent = new List<TestPaperExt>();   //定义大题的对象集(策略和其附属的小题)

            string WhereClause = "I_PaperID=" + PaperID;  //在策略表中查询试卷的策略
            TList<TbTestPaperStrategy> tempStrategy = new TestPaperStrategyDAO().GetByPage(WhereClause, 0, int.MaxValue);


            if (tempStrategy.Count > 0)   //存在该试卷的策略，同时返回该试卷的策略个数
            {
                foreach (var item in tempStrategy)
                {
                    List<QuestExt> TestLiblist = new List<QuestExt>();   //定义题库的对象集（小题和可能存在的选项）//解决问题所在 9.19
                    TestPaperExt tem = new TestPaperExt();
                    tem.StrategyItem = item;//获取当前的策略

                    string whereClause1 = "I_StrategyID=" + item.Id;
                    TList<TbTestLibPaperDetail> tempDetailList = new TestLibPaperDetailDAO().GetByPage(whereClause1, 0, int.MaxValue); //获取每一个大题中的小题题id
                    if (tempDetailList.Count > 0)
                    {
                        foreach (var item1 in tempDetailList)
                        {

                            QuestExt TtempList = new QuestExt();//获取试题集
                            string libID = item1.ILibId.ToString();
                            int LibID = int.Parse(libID);
                            TbTestLib tempLib = new TestLibDAO().GetByID(LibID); //获取小题的详细信息
                            if (tempLib != null)
                            {
                                TtempList.LibItem = tempLib;   //把每一个试题放到list中
                                var problemtype = tempLib.IProblemType;  //获取策略的大题类型
                                if (problemtype == 1 || problemtype == 2) //单选题或者多选题
                                {
                                    string WhereClause1 = "I_LibID=" + tempLib.Id;
                                    TList<TbTestLibOption> optionLists = new TestLibOptionDAO().GetByPage(WhereClause1, 0, int.MaxValue);//获取该题对应的选项
                                    if (optionLists.Count > 0)  //该题存在选项
                                    {
                                        TtempList.OptionList = optionLists;
                                    }
                                }

                            }
                            TestLiblist.Add(TtempList);//获取选项集及试题

                        }
                        tem.LibItemList = TestLiblist;//获取当前策略的附属题集 

                    }

                    tempStrategyContent.Add(tem);  //该策略 
                }


            }
            //返回策略的详细信息，返回策略的个数
            ViewBag.TestPaperDetail = tempStrategyContent;
            return View();

        }



        /// <summary>
        /// 替换个别小题
        /// </summary>
        /// <param name="CourseID">课程Id</param>
        /// <param name="ProblemType">试题类型</param>
        /// <param name="PaperDetailId">当前需修改的试题Id</param>
        /// <returns></returns>
        public ActionResult ChangeQuestionView(int CourseID, int ProblemType, int PaperDetailId, int IStrategyID)
        {
            ViewBag.CourseID = CourseID;
            ViewBag.IProblemType = ProblemType;
            ViewBag.PaperDetailId = PaperDetailId;
            ViewBag.IStrategyID = IStrategyID;
            frontCurrentTea.SetCurrentStrategyID(IStrategyID);//获取当前策略Id
            //新增部分，传递paperID
            TbTestPaperStrategy tempStrategy = new TestPaperStrategyDAO().GetByID(IStrategyID);
            if (tempStrategy != null)
            {
                ViewBag.PaperID = tempStrategy.IPaperId;
            }

            string whereClause = "I_ParentID=0  and I_DeanCourseID=" + CourseID;
            ViewBag.Charpter = new TeaCatalogDAO().GetByPage(whereClause, 0, int.MaxValue);
            return View();
        }



    }
}
